package com.taxi.driver.module.recruit.selectcar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.taxi.driver.R;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.data.entity.CarModleEntity;
import com.taxi.driver.widget.slide.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 选择车型
 * Created by Curry on 2018/5/17.
 */

public class SelectCarModleActivity extends BaseActivity
{
    @BindView(R.id.lv_slide)
    ListView lv_slide;
    @BindView(R.id.lv_main)
    ListView car_list;
    @BindView(R.id.tv_dialog)
     TextView mTvDialog;
    @BindView(R.id.sidebar)
     SideBar mSideBar;
    private CarListAdapter mAdapter;
    CarModleEntity carModleEntity;

    private List<CarModleEntity> mCars;
    public static void startIntent(Context context) {
        Intent intent = new Intent(context, SelectCarModleActivity.class);
//        intent.putExtra(KEY_ADDRESS_TYPE, type);
//        intent.putExtra(KEY_CAR_TYPE, carType);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_car);
        ButterKnife.bind(this);
        mSideBar.setTvDialog(mTvDialog);
        initEvent();

    }

    private void initEvent() {
        carModleEntity = new CarModleEntity();
        mCars = fillData(getResources().getStringArray(R.array.title));
        Collections.sort(mCars, carModleEntity);
        mAdapter = new CarListAdapter(mCars, this);
        mSideBar.setOnTouchLetterChangeListener(new SideBar.OnTouchLetterChangeListener() {
            @Override
            public void letterChange(String s) {
                int position = mAdapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    car_list.setSelection(position);
                }
            }
        });
        car_list.setAdapter(mAdapter);
        car_list.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                lv_slide.setVisibility(View.VISIBLE);

                lv_slide.setAdapter(mAdapter);

//                Toast.makeText(MainActivity.this,mFriends.get(position).getName()+"-"+position,Toast.LENGTH_SHORT).show();
            }
        });
        lv_slide.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                lv_slide.setVisibility(View.INVISIBLE);
                Toast.makeText(SelectCarModleActivity.this,mCars.get(position).getName()+"-"+position,Toast.LENGTH_SHORT).show();

            }
        });
    }

    private List<CarModleEntity> fillData(String[] data) {
        List<CarModleEntity> entities = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            CarModleEntity sortEntity = new CarModleEntity();
            sortEntity.setName(data[i]);
            sortEntity.setSortLetters(data[i].substring(0, 1).toUpperCase());
            entities.add(sortEntity);
        }
        return entities;
    }
}
